#include "sensormodel.h"
#include <QDebug>


SensorModel::SensorModel(QObject *parent)
    : QAbstractListModel(parent)
{

    m_roleNames[  NAME          ]=  "name"          ;
    m_roleNames[  VALID         ]=  "valid"         ;
    m_roleNames[  VALUE         ]=  "value"         ;
    m_roleNames[  FORMATE       ]=  "formate"       ;
    m_roleNames[  VRESOLUTION   ]=  "vresolution"   ;
    m_roleNames[  UNIT_INDEX    ]=  "unit_index"    ;
    m_roleNames[  UNIT          ]=  "unit"          ;
    m_roleNames[  MAXVALUE      ]=  "maxvalue"      ;
    m_roleNames[  MINVALUE      ]=  "minvalue"      ;
    m_roleNames[  MAXCUTOFF     ]=  "maxcutoff"     ;
    m_roleNames[  MINCUTOFF     ]=  "mincutoff"     ;
    m_roleNames[  DEVIDE        ]=  "devide"        ;

}

int SensorModel::rowCount(const QModelIndex &parent) const
{
    // For list models only the root node (an invalid parent) should return the list's size. For all
    // other (valid) parents, rowCount() should return 0 so that it does not become a tree model.
    if (parent.isValid())
        return 0;

    // FIXME: Implement me!
    //jr
    qDebug()<<"row count"<<dataList.size();
    return dataList.size();
}

QVariant SensorModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    // FIXME: Implement me!
    //jr
    int row = index.row();
    qDebug()<<"row"<<row;
     if(row < 0 || row >= dataList.count()) {
         return QVariant();
     }

     const Data &data = dataList[row];
    switch (role) {
        case NAME           : return    data.name        ; break;
        case VALID          : return    data.valid       ; break;
        case VALUE          : return    data.value       ; break;
        case FORMATE        : return    data.formate     ; break;
        case VRESOLUTION    : return    data.vresolution ; break;
        case UNIT_INDEX     : return    data.unitindex   ; break;
        case UNIT           : return    data.unit        ; break;
        case MAXVALUE       : return    data.maxvalue    ; break;
        case MINVALUE       : return    data.minvalue    ; break;
        case MAXCUTOFF      : return    data.maxcutoff   ; break;
        case MINCUTOFF      : return    data.mincutoff   ; break;
        case DEVIDE         : return    data.devide      ; break;
        default:return 0;      break;
    }

    return QVariant();
}

bool SensorModel::append(DATA data)
{
    dataList.append(data);
    return true;
}


QHash<int, QByteArray> SensorModel::roleNames() const
{
    return m_roleNames;
}
